<template>
  <!-- 添加或修改舱单申报头对话框 -->
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="1000px"
    append-to-body
    :close-on-click-modal="false"
    @close="close"
    class="self-dialog"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="158px"
      label-position="right"
      class="flex-form"
      size="small"
    >
      <el-form-item label="运输批次号" prop="manifestId">
        <el-input
          v-model.trim="form.manifestId"
          placeholder="请输入运输批次号"
        />
      </el-form-item>
      <el-form-item label="进出口类型" prop="iEFlag">
        <el-select
          v-model="form.iEFlag"
          placeholder="请选择起始监管场所"
          clearable
        >
          <el-option
            v-for="(item, index) in dict.type.mft_ieport_type"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="舱单业务编号" prop="customsStateCode">
        <el-input
          v-model.trim="form.customsStateCode"
          placeholder="请输入舱单业务编号"
        />
      </el-form-item>
      <el-form-item label="运输工具代理企业代码" prop="agentEntCode">
        <el-input
          v-model.trim="form.agentEntCode"
          placeholder="请输入运输工具代理企业代码"
        />
      </el-form-item>
      <el-form-item label="进出境口岸海关代码" prop="customsCode">
        <el-select
          v-model="form.customsCode"
          placeholder="请选择关区代码"
          filterable
          clearable
        >
          <el-option
            v-for="(item, index) in customsList"
            :key="index"
            :label="item.customsCode + '-' + item.customsName"
            :value="item.customsCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="起始监管场所编号" prop="startFieldCode">
        <el-select
          v-model="form.startFieldCode"
          placeholder="请选择起始监管场所"
          clearable
        >
          <el-option
            v-for="(item, index) in fieldOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="装载时间不能为空" prop="loadTime">
        <el-date-picker
          clearable
          size="small"
          v-model.trim="form.loadTime"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择装载时间不能为空"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="卸货点代码" prop="unloadCode">
        <el-input
          v-model.trim="form.unloadCode"
          placeholder="请输入卸货点代码"
        />
      </el-form-item>
      <el-form-item label="装货地代码" prop="loadCode">
        <el-input v-model.trim="form.loadCode" placeholder="请输入装货地代码" />
      </el-form-item>
      <el-form-item label="传输企业备案关区" prop="customMasterCode">
        <el-input
          v-model.trim="form.customMasterCode"
          placeholder="请输入传输企业备案关区"
        />
      </el-form-item>
      <el-form-item label="到达卸货地时间" prop="arrivalDate">
        <el-date-picker
          clearable
          size="small"
          v-model.trim="form.arrivalDate"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择到达卸货地时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="舱单传输人代码" prop="msgRepCode">
        <el-input
          v-model.trim="form.msgRepCode"
          placeholder="请输入舱单传输人代码"
        />
      </el-form-item>
      <el-form-item label="舱单传输人名称" prop="msgRepName">
        <el-input
          v-model.trim="form.msgRepName"
          placeholder="请输入舱单传输人名称"
        />
      </el-form-item>
      <el-form-item label="起始监管场所名称" prop="startFieldName">
        <el-input
          v-model.trim="form.startFieldName"
          placeholder="请输入起始监管场所名称"
        />
      </el-form-item>
      <el-form-item label="承运人代码" prop="carrierCode">
        <el-input
          v-model.trim="form.carrierCode"
          placeholder="请输入承运人代码"
        />
      </el-form-item>
      <el-form-item label="确报类型" prop="bindingtype">
        <el-select
          v-model.trim="form.bindingtype"
          placeholder="请选择确报类型"
          clearable
        >
          <el-option
            v-for="(item, index) in dict.type.binding_types"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model.trim="form.remark"
          type="textarea"
          placeholder="请输入内容"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  getMftManifestHead,
  addMftManifestHead,
  updateMftManifestHead,
} from "@/api/swdecl/mftManifestHead";
export default {
  name: "MftManifestHead-addEdit",
  props: {
    dialogType: {
      type: String,
      default: "add",
    },
    customsList: {
      type: Array,
      default: () => [],
    },
    fieldOptions: {
      type: Array,
      default: () => [],
    },
    dict: {}
  },
  data() {
    return {
      // 弹出层
      dialogVisible: false,
      // 标题
      title: "新增",
      rowData: {},
      // 表单参数
      form: {
        id: null,
        manifestId: null,
        iEFlag: null,
        customsStateCode: null,
        agentEntCode: null,
        customsCode: null,
        loadTime: null,
        unloadCode: null,
        loadCode: null,
        customMasterCode: null,
        arrivalDate: null,
        msgRepCode: null,
        msgRepName: null,
        startFieldCode: null,
        startFieldName: null,
        carrierCode: null,
        bindingtype: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        updateBy: null,
        remark: null,
      },
      // 舱单申报头表格数据
      mftManifestHeadList: [],
      // 表单校验
      rules: {},
    };
  },
  methods: {
    open(type, row) {
      this.dialogVisible = true;
      this.rowData = row;
      if (type == "add") {
        this.title = "新增";
      } else if (type == "edit") {
        this.loadDetail(row);
        this.title = "编辑";
      } else if (type == "detail") {
        this.loadDetail(row);
        this.title = "详情";
      }
    },
    close() {
      this.$refs["form"].resetFields();
      this.dialogVisible = false;
    },
    // 获取详情
    loadDetail() {
      const id = this.rowData.id;
      getMftManifestHead(id).then((res) => {
        if (res.code == 200) {
          this.form = res.data;
        }
      });
    },
    // 提交表单
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let params = {
            ...this.form,
          };
          if (this.dialogType == "add") {
            addMftManifestHead(params).then((res) => {
              if (res.code === 200) {
                this.$modal.msgSuccess("新增成功");
                this.close();
                this.$emit("handleQuery");
              }
            });
          } else {
            params.id = this.rowData.id;
            updateMftManifestHead(params).then((res) => {
              if (res.code === 200) {
                this.$modal.msgSuccess("修改成功");
                this.close();
                this.$emit("handleQuery");
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.flex-form {
  ::v-deep .el-select {
    width: 100%;
  }
}
</style>
